
#include "public_filelist.h"

#include "../public_string/public_string.h"

//*************************************************************************************
int filelist_get_filename(filelist_para_t * f_para,char * filename,int index)
{
	int ret   = -1;
	int total = 0;
	
	cJSON *arr_info = NULL;

	cJSON * c_filename   = NULL;
	cJSON * c_filetype   = NULL;
	cJSON * c_is_support = NULL;


	if(!filename){
		debug_e("filename error\n");
		return -1;
	}

	if(!f_para){
		debug_e("f_para error\n");
		return -1;
	}

	if(!f_para->f_cMedia){
		debug_e("f_cMedia error\n");
		return -1;
	}
	//**********************************************************
	total = cJSON_GetArraySize(f_para->f_cMedia);

	if(index >= total){
		debug_n("error index=%d\n",index);
		return -1;
	}

	arr_info   = cJSON_GetArrayItem(f_para->f_cMedia, index);
	//**********************************************************

	c_filename   = cJSON_GetObjectItem(arr_info, "fileName");
	c_filetype   = cJSON_GetObjectItem(arr_info, "filetype");
	c_is_support = cJSON_GetObjectItem(arr_info, "is_support");

	if(c_filename)
	{
		sprintf(filename,"%s",c_filename->valuestring);
	}	

#if 0
	if(c_filetype)
	{
		//debug_n("filetype=%d\n",c_filetype->valueint);
	}
#endif

	if(c_is_support)
	{
		if(!c_is_support->valueint)
		{
			debug_e("unknown support,filename=%s\n",filename);
			return -1;
		}
		else
		{
			ret = 0;
		}
	}
	else
	{
		debug_e("no  support,filename=%s\n",filename);
		return -1;
	}
	//******************************************************
	debug_n("6\n");
	
#if 1
	if(access(filename,F_OK) == 0){
		ret = 0;
	}else{
		debug_n("file not exist,%s\n",filename);
		ret = -1;
	}
#elif 0
	if(file_is_exist(filename))
	{
		ret = 0;
	}
	else
	{
		ret = -1;
	}
#endif
	debug_n("7\n");
	//******************************************************

	return ret;
}


//*************************************************************************************

int filelist_add_newfile(filelist_para_t * f_para, media_t *item)
{
	cJSON *arr_info = NULL;

	if(!f_para){
		return -1;
	}

	if(!f_para->f_cMedia){
		return -1;
	}

	arr_info = cJSON_CreateObject();  
	
	cJSON_AddNumberToObject(arr_info, "filetype", 	 item->filetype);
	cJSON_AddNumberToObject(arr_info, "is_support",  item->is_support);
	cJSON_AddStringToObject(arr_info, "fileName",    item->fileName);

	cJSON_AddItemToArray(f_para->f_cMedia, arr_info);	

	return 0;
}
//*************************************************************************************


int filelist_recursion_scan_file(filelist_para_t * f_para,const char * dir)
{
	int is_right = 0;
	
	DIR *dirfd		 = NULL;
	
	struct dirent	 *entry_T;
	char buff_T[sizeof(struct dirent) + 1024];

	char folder_path[1024];
	char filena_path[1024];

	media_t item ;

	//*********************************************************

	if(!f_para){
		debug_e("file para error\n");
		return -1;
	}

	dirfd = opendir(dir);
	if(!dirfd){
		debug_e("open dir error,%s\n",dir);
		return -1;
	}
	
	entry_T = (struct dirent *)buff_T;

	while (1)
	{

		entry_T = readdir(dirfd);
		if (!entry_T)
			break;

		if(entry_T->d_type == 4) //dir
		{
			if (!strcmp(entry_T->d_name, "."))
			{
				continue;
			}
			if (!strcmp(entry_T->d_name, ".."))
			{
				continue;
			}
			if (!strcmp_c(entry_T->d_name, "ALiDvr"))
			{
				continue;
			}
			if (!strcmp_c(entry_T->d_name, "ALIDVRS2"))
			{
				continue;
			}			
			if (!strcmp_c(entry_T->d_name, "recycler"))
			{
				continue;
			}
			
			if (!strcmp_c(entry_T->d_name, "system volume information"))
			{
				continue;
			}
			
			if (!strcmp_c(entry_T->d_name, "System Volume Information"))
			{
				continue;
			}	
			if (!strcmp_c(entry_T->d_name, "thumbNail"))
			{
				continue;
			}		
			//*************************************************************


			//*************************************************************

			memset(folder_path,0,sizeof(folder_path));
			
			strcpy(folder_path,dir);
			strcat(folder_path,"/");
			strcat(folder_path,entry_T->d_name);
			
			//debug_n("folder_path=%s\n",folder_path);
			//*************************************************************
			

			//*************************************************************

			filelist_recursion_scan_file(f_para,folder_path);

			//*************************************************************
		}
		else //file
		{
			//*************************************************************
			
			memset(filena_path,0,sizeof(filena_path));

			strcpy(filena_path,dir);
			strcat(filena_path,"/");
			strcat(filena_path,entry_T->d_name);

			//debug_n("filena_path=%s\n",filena_path);
			//*************************************************************
			memset(&item,0,sizeof(media_t));
			
			if(f_para->f_file_type  == FILE_TYPE_VIDEO)
			{
				if(__Valid_VideoFileType(filena_path))
				{
					is_right = 1;
	
					item.filetype	 = FILE_TYPE_VIDEO;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}	
				else
				{
					is_right = 0;
				}
			}
			else if(f_para->f_file_type  == FILE_TYPE_PHOTO)
			{
				if(__Valid_PhotoFileType(filena_path))
				{
					is_right = 1;
	
					item.filetype	 = FILE_TYPE_PHOTO;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}	
				else
				{
					is_right = 0;
				}
			}
			else if(f_para->f_file_type  == FILE_TYPE_VIDEO_PHOTO)
			{
				if(__Valid_VideoFileType(filena_path))
				{
					is_right = 1;
	
					item.filetype	 = FILE_TYPE_VIDEO;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}					
				else if(__Valid_PhotoFileType(filena_path))
				{
					is_right = 1;
	
					item.filetype	 = FILE_TYPE_PHOTO;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}
				else
				{
					is_right = 0;
				}
			}
			else if(f_para->f_file_type  == FILE_TYPE_MUSIC)
			{
				if(__Valid_MusicFileType(filena_path))
				{
					is_right = 1;
	
					item.filetype	 = FILE_TYPE_MUSIC;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}	
				else
				{
					is_right = 0;
				}
			}	
			else
			{
				if(__Valid_PhotoFileType(filena_path))
				{
					is_right = 1;	
				
					item.filetype	 = FILE_TYPE_PHOTO;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}
				else if(__Valid_VideoFileType(filena_path))
				{
					is_right = 1;
				
					item.filetype	 = FILE_TYPE_VIDEO;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}			
				else if(__Valid_MusicFileType(filena_path))
				{
					is_right = 1;
					
					item.filetype	 = FILE_TYPE_MUSIC;
					item.is_support  = 1;
					sprintf(item.fileName,"%s",filena_path);
				}
				else
				{
					is_right = 0;
				}
			}
			if(is_right)
			{
				f_para->f_total++;
				
				debug_n("lf=%d:%s\n",f_para->f_total,item.fileName);
				
				filelist_add_newfile(f_para,&item);	
			}
			//*************************************************************
		}
	}
	
	closedir(dirfd);
	dirfd = NULL;
	
	return 0;
}



int filelist_search_file(filelist_para_t * f_para)
{

	if(!f_para){
		debug_e("file para error\n");
		return -1;
	}

	if(!f_para->f_dir){
		debug_e("file para error\n");
		return -1;
	}

	filelist_recursion_scan_file(f_para,f_para->f_dir);


	return 0;
}


static void filelist_search_task(void *para)
{
	int i = 0;
	char file_path[1024];

	filelist_para_t * f_para = NULL;

	f_para = (filelist_para_t *)para;
	if(!f_para){
		debug_e("file para error\n");
		return ;
	}

	
	debug_n("dir=%s\n",f_para->f_dir);

	while(1)
	{
		if(!f_para->f_search_flag)
		{

			//**************************************************************************
			//**************************************************************************

			filelist_search_file(f_para);
			
			f_para->f_search_flag = 1;
			//**************************************************************************
			//**************************************************************************
			//update file list
			debug_n("f_t_num=%d\n",f_para->f_t_num);

			if(f_para->f_t_num != 0)
			{
				for(i=f_para->f_t_num-1;i>=0;i--)
				{
					cJSON_DeleteItemFromArray(f_para->f_cMedia,i);
					f_para->f_total --;
				}
			}
			//*********************************************************************************************************

			if(f_para->f_cRoot){
				cJSON_ReplaceItemInObject(f_para->f_cRoot, "filenum", cJSON_CreateNumber(f_para->f_total));
			}
			//*********************************************************************************************************
			#if 1
			memset(file_path,0,sizeof(file_path));
			
			sprintf(file_path,"%s//%s",f_para->f_dir,f_para->f_save_path);
			
			filelist_write_json(f_para->f_cRoot,file_path);
			#endif
			//**************************************************************************
			debug_n("lf=%d\n",f_para->f_total);
			//**************************************************************************
		}
		else
		{
			usleep(100*1000);
		}
	}


}






int filelist_write_json(cJSON *c_root, const char *filename)
{

	int ret       = -1;
	int fd_net_w  = -1;
	
	char * local_str 	    = NULL;

	if(!c_root){
		return -1;
	}	

	local_str = cJSON_Print(c_root);
	if(!local_str){
		return -1;
	}
	
	ret = remove(filename);
	
	fd_net_w = open(filename, O_CREAT|O_WRONLY);
	if(fd_net_w == (-1)){
		debug_e("open file %s error\n",filename);
		goto free_mem;
	}

	write(fd_net_w,local_str,strlen(local_str));

	close(fd_net_w);
	fd_net_w = -1;	 

	if(local_str){
		cJSON_free(local_str);
		local_str = NULL;
	}

	return 0;
	
free_mem:
	if(local_str){
		cJSON_free(local_str);
		local_str = NULL;
	}

	return -1;

}





filelist_para_t * filelist_cjson_create(const char * dir,int file_type,char * filepath)
{
	int  	  ret = -1;
	int       need_create = 0;
	uint64_t  file_size   = 0;

	FILE *fd = NULL;
	
	char * list_str = NULL;
	
	char file_path[1024];

	filelist_para_t * f_para = NULL;
	//*****************************************************************************

	if(!dir)
		return NULL;

	if(!filepath)
		return NULL;

	f_para = (filelist_para_t *)malloc(sizeof(filelist_para_t));
	if(!f_para){
		debug_e("file malloc error\n");
		return NULL;
	}
	
	f_para->f_cRoot 	    = NULL;
	f_para->f_file_type 	= file_type;
	f_para->f_search_flag 	= 0;
	f_para->f_total     	= 0;
	f_para->f_task_hand 	= NULL;

	f_para->f_t_num 		= 0;
	f_para->f_total 		= 0;

	memset(f_para->f_dir,0,sizeof(f_para->f_dir));
	memset(f_para->f_save_path,0,sizeof(f_para->f_save_path));
	
	sprintf(f_para->f_dir,"%s",dir);
	debug_n("f_dir=%s\n",f_para->f_dir);

	sprintf(f_para->f_save_path,"%s",filepath);
	debug_n("f_save_path=%s\n",f_para->f_save_path);
	//*****************************************************************************
	memset(file_path,0,sizeof(file_path));

	sprintf(file_path,"%s//%s",f_para->f_dir,f_para->f_save_path);

	debug_n("file_path=%s\n",file_path);
	//*****************************************************************************
	if(file_is_exist(file_path))
	{
		debug_n("file exist\n");

		fd	= fopen(file_path, "rb");	
		if(fd == NULL)
		{
			debug_n("file open error\n");
			need_create = 1;
		}
		else
		{
			//*************************************************************************
			fseek(fd, 0, SEEK_END);   
			file_size = ftell(fd);	  
			fseek(fd,0,SEEK_SET);  
			//*************************************************************************
			debug_n("file_size=%lld\n",file_size);

			if(file_size > 0)
			{
				list_str = malloc(file_size);
				if(list_str)
				{
					memset(list_str, '\0', file_size);
					fread(list_str,1,file_size,fd);
				}
				//*************************************************************************
				fclose(fd);
				fd = NULL;
				
				//*************************************************************************
				f_para->f_cRoot = cJSON_Parse(list_str);  
				
				free(list_str);
				list_str = NULL;
				//*************************************************************************
				if(f_para->f_cRoot)
				{
					f_para->f_cMedia = cJSON_GetObjectItem(f_para->f_cRoot, "media");
					if(f_para->f_cMedia)
					{
						f_para->f_total  = cJSON_GetArraySize(f_para->f_cMedia);
						f_para->f_t_num  = f_para->f_total;	
						
						need_create = 0;
					}
					else
					{
						need_create = 1;
						cJSON_Delete(f_para->f_cRoot);
						f_para->f_cRoot = NULL;
					}
				}
				else
				{
					need_create = 1;
				}
				//*************************************************************************
			}
			else
			{
				fclose(fd);
				fd = NULL;
				//*************************************************************************
				need_create = 1;
				//*************************************************************************
			}
		}
	}
	else
	{
		need_create = 1;
	}

	debug_n("need_create=%ld\n",need_create);
	//*****************************************************************************
	if(need_create)
	{
		f_para->f_cRoot = cJSON_CreateObject();	
		
		cJSON_AddStringToObject(f_para->f_cRoot, "name",     "local_media");	
		cJSON_AddNumberToObject(f_para->f_cRoot, "filenum",  f_para->f_total);
		
		
		f_para->f_cMedia = cJSON_CreateArray();			
		cJSON_AddItemToObject(f_para->f_cRoot, "media", f_para->f_cMedia);	

		f_para->f_t_num = 0;
		f_para->f_total = 0;
		//*****************************************************************************
	}

	ret=pthread_create(&f_para->f_task_hand, NULL, filelist_search_task, f_para);
	if(ret != 0){
		log_e("error=%s\n",strerror(ret));
	}

	usleep(100*1000);

	return f_para;
}

int filelist_cjson_destory(filelist_para_t * f_para)
{
	if(!f_para){
		return -1;
	}

	debug_n("file list delete\n");

	if(f_para->f_cRoot){
		cJSON_Delete(f_para->f_cRoot);
		f_para->f_cRoot = NULL;
		f_para->f_cMedia = NULL;  
	}

#if 0
	if(f_para->f_task_hand){
		vTaskDelete(f_para->f_task_hand);
		f_para->f_task_hand = NULL;
	}
#endif

	if(f_para){
		free(f_para);
		f_para = NULL;
	}

	return 0;
}


























